<--- %%NOBANNER%% --> _ranuni.sas
 BackForward

/*-------------------<-- Start of Description -->--------------------\
| Generate random variate from a uniform distribution                |
|---------------------<-- End of Description -->---------------------|
|--------------------------------------------------------------------|
|---------------<-- Start of Files or Arguments Needed -->-----------|
| Arguments Need:                                                    |
|      seed  - seed; Required, default is the current system time;   |
|      var   - the variable to save the generated random variates;   |
|      lower - the lower limit;                                      |
|      upper - the uppder limit;                                     |
|      temp  - the temporary variable to update the seed;            |
|              default is _ranuni0_;                                 |
|----------------<-- End of Files or Arguments Needed -->------------|
|--------------------------------------------------------------------|
|-----------------<-- Start of Example and Usage -->-----------------|
|Example                                                             |
|   data one;                                                        |
|      do i=1 to 20;                                                 |
|      x=%_ranuni(lower=1, upper=2);                                 |
|      %_ranuni(seed=12345,var=y, lower=1, upper=2);                 |
|      output;                                                       |
|      end;                                                          |
|   run; %print(one);                                                |
|Usage: _ranuni(seed=%sysfunc(datetime(), 15.), var=, lower=, upper=,|
|               temp=_ranuni0_);                                     |
\--------------------<-- End of Example and Usage -->---------------*/
%macro _ranuni(seed=%sysfunc(datetime(), 15.), var=, lower=, upper=, 
               temp=_ranuni0_);
/*--------------------------------------------\
| Author:  Duo Zhou;                          |
| Created: 2-17-2002 6:30pm;                  |
| Purpose: Random Uniform Generator;          |
\--------------------------------------------*/
%if (%quote(&seed) eq) %then %do;
   %put ==> Error: This is not a valid seed!; 
   %if (%length(&var)) %then %do; &var=.; %end;
   %else %do; .;%end;
   %goto finish;
%end;
%else %do;
    %if (%length(&var)) %then %do;
        %if (not %sysfunc(rxmatch(%sysfunc(rxparse(_|.|$a|$A|$w)),&seed))) %then %do;
            drop &temp;
            retain &temp &seed;

            %let seed=&temp;
        %end;
        
        call ranuni(&seed, &var);
        
        %if (%length(&lower.&upper)) %then &var=&var;
    %end;
    %else ranuni(&seed);
        
    %if (%length(&upper)) %then %do;
        %if (%length(&lower)) %then *(&upper-&lower);
        %else *&upper;
    %end;
    
    %if (%length(&lower)) %then +&lower;
%end;
%finish:
%mend _ranuni;